package com.info3k.client.commands;

import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl3D;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LatLngCallback;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.info3k.client.Info3K;
import com.info3k.client.components.InfoWindow;

public class GoogleMapsCommand implements Command {
	
	private String page;
	
	private Geocoder geocoder = new Geocoder();
	
	private MapWidget map;
	
	private VerticalPanel panel = new VerticalPanel();
	
	private TextBox street = new TextBox();
	
	public GoogleMapsCommand(String page){
		this.page = page;
	}
	
	public void execute() {
		Info3K.loader.setVisible(true);
		RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, page);
		requestBuilder.setCallback(new RequestCallback(){
			public void onError(Request request, Throwable exception) {
				Info3K.loader.setVisible(false);
				InfoWindow.showMessage(Info3K.msgs.error(), exception.getMessage());
			}

			public void onResponseReceived(Request request, Response response) {
				Info3K.loader.setVisible(false);
				
				RootPanel.get("right_section").clear();
				RootPanel.get("right_section").add(new HTML(response.getText()));
				
				LatLng dzela = LatLng.newInstance(43.850488,18.398074);
				
				map = new MapWidget(dzela, 18);
			    map.setSize("615px", "520px");
			    
			    map.addControl(new LargeMapControl3D());
			    
			    map.addOverlay(new Marker(dzela));
			    
			    map.setCurrentMapType(MapType.getSatelliteMap());

			    map.getInfoWindow().open(map.getCenter(), new InfoWindowContent("Dzela"));
			    
			    street.setStylePrimaryName("field");
			    street.addKeyPressHandler(new KeyPressHandler(){
					public void onKeyPress(KeyPressEvent event) {
						if(event.getCharCode() == 13){
						    geocoder.getLatLng(street.getText(), new LatLngCallback(){
								public void onFailure() {
									// TODO Auto-generated method stub
								}

								public void onSuccess(LatLng point) {
									if(point!=null){
										map.setCenter(point);
										map.setZoomLevel(10);
										map.getInfoWindow().open(map.getCenter(), new InfoWindowContent(street.getText()));
									}
								}
						    	
						    });
						}
					}
			    	
			    });
			    
			    panel.add(street);
			    panel.add(map);
			    
			    RootPanel.get("map").add(panel);
			}
		});
		try {
			requestBuilder.send();
		} catch (RequestException e) {
			Info3K.loader.setVisible(false);
			InfoWindow.showMessage(Info3K.msgs.error(), e.getMessage());
		}
	}
}
